package cn.jiyun.controller;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("addUser")
    public String addUser(User user){
        userService.add(user);
        return "list";
    }
    @RequestMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/listUser";
    }
    /**
     * 页面跳转
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }
    /**
     * 查询全部用户
     */
    @RequestMapping("/listUser")
    public String listUser(Model model) {
        List<User> list = this.userService.listUser();
        model.addAttribute("list", list);
        return "list";
    }
    @RequestMapping("/findId")
    public String listUser(Integer id,Model model) {
        User user1 = this.userService.findId(id);
        model.addAttribute("user1", user1);
        System.out.println(user1);
        return "update";
    }
    @RequestMapping("/delete")
    public String delete(Integer id) {
        userService.delete(id);
        return "redirect:/user/listUser";
    }
    /**
     * 登录功能
     */
    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
         User loginuser =userService.login(user);
              if (loginuser!=null){
                  session.setAttribute("loginuser",loginuser);
                  return "redirect:/user/listUser";
              }
              return "tologin";

    }
}
